package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.dto.ApplyDto;
import com.zhentao.mapper.*;
import com.zhentao.pojo.*;
import com.zhentao.service.PointsApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author hp
* @description 针对表【points_apply(积分申请表)】的数据库操作Service实现
* @createDate 2025-07-09 15:20:23
*/
@Service
public class PointsApplyServiceImpl extends ServiceImpl<PointsApplyMapper, PointsApply>
    implements PointsApplyService{
    @Autowired
    private PointsApplyMapper pointsApplyMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private PointsRuleMapper pointsRuleMapper;
    @Autowired
    private RewardTypeMapper rewardTypeMapper;
    @Autowired
    private PunishTypeMapper punishTypeMapper;
    @Override
    public Page<PointsApply> listPendingApply(ApplyDto applyDto) {
        Page<PointsApply> page = new Page<>(applyDto.getCurrent(),applyDto.getSize());
        QueryWrapper<PointsApply> pointsApplyQueryWrapper = new QueryWrapper<>();
        pointsApplyQueryWrapper.eq(applyDto.getTeacherId()!=null,"teacher_id",applyDto.getTeacherId());
        pointsApplyMapper.selectPage(page,pointsApplyQueryWrapper);
        List<PointsApply> records = page.getRecords();
        if(records!=null&&records.size()>0){
            for (PointsApply pa:records
                 ) {
                Integer teacherId = pa.getTeacherId();
                Integer studentId = pa.getStudentId();
                Teacher teacher = teacherMapper.selectById(teacherId);
                Student student = studentMapper.selectById(studentId);
                pa.setStudent(student);
                pa.setTeacher(teacher);

                PointsRule rule = pointsRuleMapper.selectById(pa.getRuleId());
                if ("加分".equals(rule.getRuleType())) {
                    List<RewardType> rewardList = rewardTypeMapper.selectList(
                            new QueryWrapper<RewardType>().eq("rule_id", pa.getRuleId()));
                    if (rewardList != null && !rewardList.isEmpty()) {
                        pa.setRule(rewardList.get(0));
                    }
                } else {
                    List<PunishType> punishList = punishTypeMapper.selectList(
                            new QueryWrapper<PunishType>().eq("rule_id", pa.getRuleId()));
                    if (punishList != null && !punishList.isEmpty()) {
                        pa.setRule(punishList.get(0));
                    }
                }
            }
            return page;
        }
        return null;
    }

    @Override
    public Page<PointsApply> findMyApplyList(Integer teacherId, int pageNum, int pageSize) {
        Page<PointsApply> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<PointsApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PointsApply::getTeacherId, teacherId); // 这里字段名要和实体类一致
        wrapper.orderByDesc(PointsApply::getCreateTime);
        this.page(page, wrapper);
        List<PointsApply> records = page.getRecords();
        if(records != null && !records.isEmpty()) {
            for (PointsApply pa : records) {
                Integer studentId = pa.getStudentId();
                Student student = studentMapper.selectById(studentId);
                pa.setStudent(student);
            }
        }
        return page;
    }


}




